package com.ming.reggie.controller;

import com.ming.reggie.common.R;
import com.ming.reggie.dto.SetmealDto;
import com.ming.reggie.entity.Page;
import com.ming.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    @Autowired(required = false)
    private HttpSession session;

    /**
     * 处理添加套餐请求
     * @param setmealDto
     * @return
     */
    @RequestMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        Long id = (Long) session.getAttribute("employee");

        setmealDto.setUpdateUser(id);
        setmealDto.setCreateUser(id);

        setmealService.save(setmealDto);

        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> findByPage(Integer page,Integer pageSize,String name){
        Page<SetmealDto> page1 = setmealService.findByPage(page, pageSize, name);

        return R.success(page1);
    }

    //@RequestParam可以将前端传过来的参数转化成list集合,不加默认转换成数组
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        setmealService.deleteByIds(ids,id);

        return R.success("删除套餐成功");
    }
}
